import sys
import os
import string
import secrets
import jwt
import datetime




from pathlib import Path
# 动态添加utils文件夹的路径
current_file_path = Path(os.path.abspath(__file__))
parent_dir = current_file_path.parent.parent
print(parent_dir)
sys.path.append(os.path.join(parent_dir, 'models'))
from  models  import  assistantLoginModel
from utils  import AuthTokenManager







def assistantLogin_homePage():
    """查询养老机构基本信息表和生成初始化验证码。

        Args:


        Returns:
            institutionList: 养老机构列表
            init_verification_code: 初始化验证码


    """

    institutionList =[]
    result = assistantLoginModel.query_institution_Information()
    for item in result :
        print("  item  =  ")
        print(item)

        print("  item['institutionName']  =  ")
        print(item['institutionName'])
        data = {
            item['id']: item['institutionName']
        }
        institutionList.append(data)

    print(institutionList)

    verification_code = init_assistantLogin_verification()

    result = {
        "institutionList": institutionList,
        "init_verification_code": verification_code
    }



    return result

def init_assistantLogin_verification():
    """随机生成验证码。
         Args:
         Returns:
             verification_code: 随机生成的验证码

    """

    six_digit_random = ''.join(secrets.choice(string.digits) for _ in range(4))
    print(six_digit_random)  # 输出：如 "819203"

    # 创建字典
    data = {
        "verification_code": six_digit_random
    }

    return data

def assistantLogin_verification(tokenId, username):
    """随机生成验证码。
         Args:
         Returns:
             verification_code: 随机生成的验证码

    """

    # tokenIdObj = assistantLoginModel.query_insPersonLoginInfo_tokenid(username)
    # tokenId = tokenIdObj['tokenId']
    # verifyResult = AuthTokenManager.verifyToken(tokenId, username)
    # print("   verifyResult   =   ")
    # print(verifyResult)

    # data = {
    #     "tokenId": 0,
    #     "verification_code": 0
    # }

    # if  verifyResult == 1 :
    #     newTokenId = AuthTokenManager.generateToken(username, 2, 0)
    #     print("  newTokenId =   ")
    #     print(newTokenId)
    #
    #     six_digit_random = ''.join(secrets.choice(string.digits) for _ in range(4))
    #     print(six_digit_random)  # 输出：如 "819203"
    #
    #     # 创建字典
    #     data = {
    #         "tokenId": newTokenId,
    #         "verification_code": six_digit_random
    #     }

    six_digit_random = ''.join(secrets.choice(string.digits) for _ in range(4))

    data = {
        "tokenId": 0,
        "verification_code": six_digit_random
    }

    return data


def assistantLogin_login(institutionId,username,password):
    """登录页面。
         Args:
          :功能: 登录页面
          :institutionId : 养老机构Id
         ：username：  用户名
         ：password ： 密码
         :return: 成功返回 1    失败返回  0    失败原因

         Returns:
             result: 成功： success   失败: false

    """
    count = assistantLoginModel.count_insPersonLoginInfo(institutionId,username,password)


    tokenId = AuthTokenManager.generateToken(username,0,2)     # 设置0天 2小时
    # tokenUpdate  = assistantLoginModel.update_insPersonLoginInfo_tokenid(username , tokenId)
    # if  tokenUpdate == 0 :
    #     result = 0
    #     reason = "tokenId 生成或者保存失败！"
    #     data = {
    #         "result": result,
    #         "reason": reason
    #     }
    #
    #     return  data

    result =  0
    reason = "请检查机构、账号和密码是否正确"
    data = {
        "result": result,
        "reason": reason,
        "tokenId": None
    }
    if  count == 1 :
        result = 1
        data = {
            "result": result,
            "reason": "登录成功",
            "tokenId": tokenId
        }

    return data














